home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-04 / bipl.zip / PROGS.ZIP / FILECNVT.ICN < prev    next >
Text File  |  1992-11-26  |  3KB  |  94 lines

  1. ############################################################################
  2. #
  3. #    File:     filecnvt.icn
  4. #
  5. #    Subject:  Program to convert line terminators
  6. #
  7. #    Author:   Beth Weiss
  8. #
  9. #    Date:     January 20, 1991
  10. #
  11. ###########################################################################
  12. #
  13. #  Links: options
  14. #
  15. ############################################################################
  16. #
  17. #     This program copies a text file, converting line terminators. It is
  18. #  called in the form
  19. #
  20. #    filecnvt [-i s1] [-o s2] infile outfile
  21. #
  22. #  The file name "-" is taken to be standard input or output, depending
  23. #  on its position, although standard input/output has limited usefulness,
  24. #  since it translates line terminators according the the system
  25. #  being used.
  26. #
  27. #     The options are:
  28. #
  29. #    -i s1    assume the input file has line termination for the
  30. #            system designated by s1. The default is "u".
  31. #
  32. #    -o s2    write the output file with line terminators for the
  33. #          system designated by s2. The default is "u".
  34. #
  35. #  The designations are:
  36. #
  37. #    d    MS-DOS ("\n\r"); also works for the Atari ST
  38. #    m    Macintosh ("\r")
  39. #    u    UNIX ("\n"); also works for the Amiga
  40. #  
  41. ############################################################################
  42. #
  43. #  Links:  options
  44. #
  45. ############################################################################
  46.  
  47. link options
  48.  
  49. procedure main(args)
  50.    local T, input, output, input_eoln, output_eoln, last_part, line, result
  51.  
  52.    T := options(args, "i:o:")
  53.  
  54.    if args[1] == "-" then
  55.       input := &input
  56.    else
  57.       input := open(args[1], "ru") | stop("*** cannot open ", args[1], "***")
  58.  
  59.    if args[2] == "-" then
  60.       output := &output
  61.    else
  62.       output := open(args[2], "wu") | stop("*** cannot open ", args[2], "***")
  63.  
  64.    input_eoln  := \eoln(T["i"]) | "\n"
  65.    output_eoln := \eoln(T["o"]) | "\n"
  66.  
  67.    last_part := ""
  68.  
  69.    while line := reads(input, 10000) do {    # magic number
  70.       (last_part || line) ? {
  71.          while result := tab(find(input_eoln)) do {
  72.             writes(output, result, output_eoln)
  73.             move(*input_eoln)
  74.          }
  75.          # Saving the last part of each read and prepending it to the next
  76.          # ensures that eoln symbols that span reads aren't missed.
  77.          last_part := tab(0)
  78.       }
  79.    }
  80.  
  81.    writes(output, last_part)
  82.  
  83.    close(input)
  84.    close(output)
  85. end
  86.  
  87. procedure eoln(file_type)
  88.    case file_type of {
  89.       "u" : return "\n"
  90.       "d" : return "\r\n"
  91.       "m" : return "\r"
  92.    }
  93. end
  94.